<?php
namespace app\api\controller;

use app\common\controller\Api;
use app\common\model\UserToken;
use think\facade\Cookie;

class User extends Api {
    public function reg()
    {
        $data =[];
        $data['username'] = input('username');
        $data['password'] = input('password');
        $data['mobile'] = input('phone');
        if(empty($data['username'])){
            return showErr('请填写姓名');
        }
        if(empty($data['password'])){
            return showErr('请填写密码');
        }
        if(empty($data['mobile'])){
            return showErr('请填写手机号');
        }
        $un = new \app\common\model\User();
        $uname = $un->whereOr('username',$data['username'])->whereOr('mobile',$data['mobile'])->value('id');
        if($uname){
            return showErr('该用户已存在');
        }
        $result = $un->reg($data);
        if($result){
            $info['code'] ='1';
            $info['msg'] ='注册成功';
            return showOk($info,'注册成功');
        }else{
            return showErr('注册失败');
        }
    }

    /*
 * 登录
 */
    public function login(){
        $un = new \app\common\model\User();
        $data =[];
        $data['username'] = input('username');
        $data['password'] = input('password');
        if(empty($data['username'])){
            return showErr('请填写姓名');
        }
        if(empty($data['password'])){
            return  showErr('请填写密码');
        }
        $result = $un->login($data);
        if($result){
            $info['code'] ='1';
            $info['msg'] ='查询成功';
            $info['data'] =$result;
            return showOk($info,'查询成功');
        }else{
            $info['code'] =false;
            $info['msg'] ='未查到该用户';
            return showErr('未查到该用户');
        }
    }

   public function loginOut()
    {
        Cookie::clear('zwb_');
        $info['code'] ='1';
        $info['msg'] ='查询成功';
        $info['data'] =[];
        return showOk($info,'查询成功');
    }
}